<?php
class FotosController extends AppController {

	var $name = 'Fotos';
	var $components = array('Image', 'Uploader.Uploader');
	var $actsAs = array(
	    'Uploader.FileValidation' => array(
		'file0' => array(
		    'extension' => array(
			'value' => array('jpg', 'png', 'jpeg'),
			'error' => 'Mimetype incorrect',
		    )
		)
	    )
	);
	var $fotodir = '';
	var $previewdir = '';

	function beforeFilter() {
		parent::beforeFilter();
		$this->Auth->deny('index', 'edit', 'delete');

		$this->fotodir = APP.WEBROOT_DIR.DS.'img'.DS.'fotos';
		$this->previewdir = $this->fotodir.DS.'previews';
		$this->Uploader->uploadDir = 'photos/';
	}

	function index() {
		$data = false;
		$this->loadModel('Fotopreview');
		if ($uid = $this->Auth->user('id')) {
//pr($data);

			if (!empty($this->data)) {
				$this->Uploader->uploadDir .= $this->Auth->user('login') . '/';

				$rSide = $this->Image->getResizeSide($this->data['fotos']['file0']['tmp_name']);
				
				$hash	= md5($this->Uploader->uploadDir . date('Y-m-d H:i:s') . rand(0, 99999));
				$params = array('overwrite' => true, 'name' => $hash);
				if ($upload = $this->Uploader->upload('file0', $params)) {
					//pr ($upload);
					$fotoSave['hash']	= $hash;
					$fotoSave['user_id']	= $uid;
					$fotoSave['path']	= $upload['path'];
					$fotoSave['type']	= $upload['type'];
					$fotoSave['size']	= $upload['size'];
					$fotoSave['name']	= $upload['name'];
					$fotoSave['ext']	= $upload['ext'];
					$fotoSave['wh']		= $upload['width'] . 'x' . $upload['height'];
					$fotoSave['alt']	= '';
					$fotoSave['form']	= $rSide;

					if ($fid = $this->Foto->save($fotoSave)) {
						
						$fotopreSave['user_id'] = $uid;
						$fotopreSave['foto_id'] = $this->Foto->id;

						$fotopreSave['path']	= $this->Uploader->resize(array($rSide => 580, 'append' => '580'));
						$fotopreSave['scale']	= '580';

						$this->Foto->Fotopreview->create();
						$this->Foto->Fotopreview->save($fotopreSave);
					
						$fotopreSave['path']	= $this->Uploader->resize(array($rSide => 235, 'append' => '235'));
						$fotopreSave['scale']	= '235';

						$this->Foto->Fotopreview->create();
						$this->Foto->Fotopreview->save($fotopreSave);
					}
				}
			}

/*
			#upload image
			if (!empty($this->data['file']['file0']['tmp_name'])) {

				$file = $this->data['file']['file0'];

				if (is_uploaded_file($file['tmp_name'])) { 
					$fileData = fread(fopen($file['tmp_name'], "r"), $file['size']);

					list($width, $height) = getimagesize($file['tmp_name']);
					
					$chType = false;
					if (in_array($file['type'], $this->Image->getAllowTypes())) {
						$chType = next(explode('/', $file['type']));
					} else {
						$this->Session->setFlash('not allowed file type');
					}

					$chSize = false;
					if ($file['size'] <= $this->Foto->getAllowSize()) {
						$chSize = true;
					} else {
						$this->Session->setFlash('not allowed file size');
					}

					$resizeParams['pathtofile'] = $file['tmp_name'];
					$resizeParams['type'] = $file['type'];
					$resizeParams['filesize'] = $file['size'];
					$resizeParams['filter'] = array('selective_blur');
					$resizeParams['width'] = $width;
					$resizeParams['height'] = $height;

					$maxSizeFoto = $this->Foto->getMaxSize();
					if (list($reWidth, $reHeight) = $this->Image->getRatioSize($width, $height, $maxSizeFoto)) {
						$resizeParams['new_width'] = $reWidth;
						$resizeParams['new_height'] = $reHeight;
						$dataS = $this->Image->resize($resizeParams);
						$resizeParams['width'] = $reWidth;
						$resizeParams['height'] = $reHeight;
					} else {
						$fileData = fread(fopen($file['tmp_name'], "r"), $file['size']);
						$dataS['content']	= $fileData;
						$dataS['size']		= $file['size'];
						$dataS['wh']		= $width .'x'. $height;
					}

					$dataS['user_id']	= $uid;
					$dataS['type']		= $file['type'];
					$dataS['name']		= $file['name'];
					$dataS['status']	= 1;
					$dataS['alt']		= $this->data['file']['alt'];
					$dataS['form']		= $this->Image->getImageForm($width, $height);

					
			
					if (isset($dataS['content'])) {
						$dataS['hash'] = md5(date("Y-m-d H:i:s") . rand(0, 99999));

						//copy($file['tmp_name'], $this->fotodir.'/'.$dataS['hash'].'.'.$chType);
						if ($this->Foto->save($dataS)) {
							$maxSizePreview = $this->Fotopreview->getMaxSize();
							list($pWidth, $pHeight) = $this->Image->getRatioSize($width, $height, $maxSizePreview);

							$resizeParams['new_width'] = $pWidth;
							$resizeParams['new_height'] = $pHeight;
							$dataSP = $this->Image->resize($resizeParams);

							if (is_array($dataSP)) {
								$dataSP = array_merge($dataS, $dataSP);
							} else {
								$dataSP = $dataS;
							}
							$dataSP['foto_id'] = $this->Foto->id;
							if ($this->Fotopreview->save($dataSP)) {
								//move_uploaded_file($file['tmp_name'], $this->previewdir.'/'.$dataS['hash'].'.'.$chType);
								$this->Session->setFlash('image uploaded');
							}

							//$this->redirect('/fotos');
						}
					}
				}
			}
*/

			$data['fotos'] = $this->Foto->getFotos($uid, $this->Foto->small());
		}

		if ($data) {
			//pr($data);
			$this->set($data);
		}
	}

	function show($hash = null) {
		if ($hash) {
			$img = $this->Foto->getFoto($hash);
			if($img) {
				header('Content-type: ' . $img['Foto']['type']);
				//header('Content-length: ' . $img['Foto']['size']);
				//header('Content-Disposition: attachment; filename='.$img['Foto']['name']);
				echo $img['Foto']['content'];
			}
		}
	}

	function edit($hash = null) {
		$data = false;
		$uid = $this->Auth->user('id');
		if ($uid && $hash) {
			$data['foto'] = $this->Foto->getFoto($hash, $this->Foto->big());
			if (isset($this->data) && $data['foto']) {

				$id = $this->Foto->field('id', array('user_id'=>$uid, 'hash'=>$hash));
				if ($id) {
					$this->Foto->id = $id;
					$dataS['alt'] = $this->data['Foto']['alt'];
					if ($this->Foto->save($dataS)) {
						$this->redirect('/fotos/edit/'.$hash);
					}
				}
			}
		}

		if ($data) {
			$this->set($data);
		}
	}

	function delete($hash = null) {
		$uid = $this->Auth->user('id');
		if ($uid && $hash) {
			if ($this->Foto->deleteFoto($uid, $hash)) {
				$this->Session->setFlash('foto is deleted');
				//$this->redirect('/fotos');
			}
		}
	}

	function mainfoto($hash = null) {
		$uid = $this->Auth->user('id');
		if ($uid && $hash) {
			if ($this->Foto->setMainFoto($uid, $hash)) {
				$this->redirect('/fotos');
			}
		}
	}
}
?>
